<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Useradmin extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->title = "SMILE | Selamat datang, telkom siap melayani Anda!";

        $this->load->helper(array('date', 'url'));
        $this->layout = "admin";

        $this->scripts = array("jquery", "jquery-ui", "jquery.dataTables.min");
        $this->styles = array("jquery-ui", "demo_table", "useradmin");
    }

    public function index() {
        if ($this->session->userdata('is_super_admin')) {
            $data["users"] = $this->usermodel->get_all_user_by_super_admin()->result_array();
            $data['plasas'] = $this->plasamodel->get_all_plasa_simple()->result_array();
        } else if ($this->session->userdata('is_admin')) {
            $plasa_id = $this->session->userdata('is_admin');
            $data["users"] = $this->usermodel->get_all_user_by_plasa($plasa_id)->result_array();
            $data['plasas'] = $this->plasamodel->get_plasa_info($plasa_id)->result_array();
        } else if ($this->session->userdata('is_pic')) {
            redirect('admin/statistic');
        } else {
            redirect('admin/site/login');
        }

        $menudata['is_super_admin'] = $this->session->userdata('is_super_admin');
        $menudata['is_admin'] = $this->session->userdata('is_admin');
        $menudata['is_pic'] = $this->session->userdata('is_pic');
        $data['is_super_admin'] = $menudata['is_super_admin'];
        $data['is_admin'] = $menudata['is_admin'];
        $this->menubar = $this->load->view('admin/menubar', $menudata);
        $this->load->view("admin/user_management", $data);
    }

    public function getuser($user_id) {
        $this->layout = false;
        echo json_encode($this->usermodel->get_info_user($user_id)->row_array());
    }

    public function add() {
        $this->layout = false;

        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $email = $this->input->post('email');
        $role_id = $this->input->post('role_id');
        $plasa_id = $this->input->post('plasa_id');

        echo $this->usermodel->add_user($username, $password, $email, $role_id, $plasa_id);
    }

    public function update() {
        $this->layout = false;

        $user_id = $this->input->post('user_id');
        $username = $this->input->post('username');
        $email = $this->input->post('email');
        $role_id = $this->input->post('role_id');
        $plasa_id = $this->input->post('plasa_id');

        echo $this->usermodel->update_user($user_id, $username, NULL, $email, $role_id, $plasa_id);
    }

    public function delete() {
        $this->layout = false;

        echo $this->usermodel->delete_user($this->input->post('user_id'));
    }

    public function change_password() {
        $this->layout = false;

        $user_id = $this->input->post('user_id');
        $new_password = $this->input->post('new_password');

        echo $this->usermodel->update_password_user($user_id, $new_password);
    }

    public function get_user_by_plasa() {
        $this->layout = false;

        $result = $this->usermodel->get_all_user_by_plasa($this->input->post('plasa_id'));
        echo json_encode($result->result_array());
        //echo $result;
    }

    public function get_all_user_by_super_admin() {
        $this->layout = false;
        $result = $this->usermodel->get_all_user_by_super_admin();
        echo json_encode($result->result_array());
        //echo $result;
        //echo "kepanggil bangke version";
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */